package com.heatup.apt.common.util.Netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.*;

/**
 * Created by blank on 2017/11/17 上午10:27
 */
public class HttpAggregatorInitalizer extends ChannelInitializer<Channel> {

    private final boolean client;

    public HttpAggregatorInitalizer(boolean client) {
        this.client = client;
    }

    @Override
    protected void initChannel(Channel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        if (client) {
            pipeline.addLast("codec", new HttpClientCodec()); //client: 添加 HttpClientCodec
            pipeline.addLast("decompressor", new HttpContentDecompressor()); //client: 添加 HttpContentDecompressor 用于处理来自服务器的压缩的内容
        } else {
            pipeline.addLast("codec", new HttpServerCodec()); //server: 添加 HttpServerCodec 作为我们是 server 模式时
            pipeline.addLast("compressor", new HttpContentCompressor()); // server: HttpContentCompressor 用于压缩来自 client 支持的 HttpContentCompressor
        }
        pipeline.addLast("aggegator", new HttpObjectAggregator(512 * 1024)); //添加 HttpObjectAggregator 到 ChannelPipeline, 使用最大消息值是 512kb
    }
}
